package org.xbug.search.woody.core.util;

import java.util.HashMap;
import java.util.Map;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

/**
 * fastjson 快速取值
 * 
 * @author xuzhengtao
 *
 */
public class FastJsonExt {

	/**
	 * 必达性值
	 * @param key
	 * @param obj
	 * @return
	 */
	@Deprecated
	public static String getString(String key, JSONObject obj) {
		return get(key,obj).toString();
	}

	/**
	 * 必达性值
	 * @param key
	 * @param obj
	 * @return
	 */
	@Deprecated
	public static Object get(String key, JSONObject obj) {
		int index = key.lastIndexOf(".");
		if (index == -1) {
			return obj.get(key);
		}
		return getJson(key.substring(0, index), obj).get(key.substring(index+1));
	}

	/**
	 * 必达性值
	 * @param key
	 * @param obj
	 * @return
	 */
	@Deprecated
	public static int getIntValue(String key, JSONObject obj) {
		return Integer.parseInt(get(key,obj).toString());
	}

	/**
	 * 必达性值
	 * @param key
	 * @param obj
	 * @return
	 */
	@Deprecated
	public static double getDoubleValue(String key, JSONObject obj) {
		return Double.parseDouble(get(key,obj).toString());
	}

	@Deprecated
	private static JSONObject getJson(String key, JSONObject obj) {
		String[] keys = key.split("\\.");
		for (String k : keys) {
			int[] type = switchType(k);
			if (type[0] == arr) {
				JSONArray arry = null;
				for (int i = 0; i < type.length; i++) {
					if (i == 0) {
						arry = obj.getJSONArray(k.substring(0, k.indexOf('[')));
						continue;
					}

					if (i == type.length - 1) {
						obj = arry.getJSONObject(type[i]);
						break;
					}
					
					arry = arry.getJSONArray(type[i]);
				}
			} else {
				obj = obj.getJSONObject(k);
			}
		}
		return obj;
	}
	
	/**
	 * 获得key 路径深度的json字符串
	 * @param key
	 * @param obj
	 * @return
	 */
	public static Object getChild(String key, JSONObject obj) {
		String[] keys = key.split("\\.");
		Object result = null;
		JSONArray arry = null;
		for(String k : keys) {
			int enbIndex = k.lastIndexOf(']');
			if(enbIndex == -1) {
				result = obj.get(k);
				if(result instanceof JSONObject) {
					obj = (JSONObject) result; 
				}
			}else {
				int startIndex = k.indexOf('[');
				String pre = k.substring(0,startIndex);
				arry = obj.getJSONArray(pre);
				String[] xiabiao = k.substring(startIndex + 1,enbIndex).split("\\]\\[");
				for(int i=0;i<xiabiao.length;i++) {
					int x = Integer.parseInt(xiabiao[i]);
					Object o = arry.get(x);
					if(o instanceof JSONObject) {
						obj = (JSONObject) o;
					}else if(o instanceof JSONArray){
						arry = (JSONArray) o;
					}
					result = o;
				}
			}
		}
		
		return result;
	}
	
	public static String getChildString(String key, JSONObject obj) {
		return (String) getChild(key,obj);
	}
	
	public static String getChildJSONString(String key, JSONObject obj) {
		Object o = getChild(key,obj);
		return o==null?"":JSONObject.toJSONString(o);
	}
	
	public static JSONObject getChildJSON(String key, JSONObject obj) {
		return (JSONObject)getChild(key,obj);
	}
	
	public static JSONArray getChildJSONArray(String key, JSONObject obj) {
		return (JSONArray)getChild(key,obj);
	}
	
	public static Integer getChildIntValue(String key, JSONObject obj) {
		Object o = getChild(key,obj);
		return o==null?null:Integer.valueOf((String)o);
	}
	
	public static Double getChildDoubleValue(String key, JSONObject obj) {
		Object o = getChild(key,obj);
		return o==null?null:Double.parseDouble((String)o);
	}

	private static int obj = 0;
	private static int arr = 1;

	@Deprecated
	private static int[] switchType(String key) {
		int[] result = null;
		int arrayEndIndex = key.lastIndexOf(']');
		if (arrayEndIndex != -1) {// is array
			int arrayStartIndex = key.indexOf('[');
			String[] xiabiao = key.substring(arrayStartIndex + 1, arrayEndIndex).split("\\]\\[");
			result = new int[xiabiao.length + 1];
			result[0] = arr;
			for (int x = 1; x <= xiabiao.length; x++) {
				result[x] = Integer.parseInt(xiabiao[x - 1]);
			}

		} else {
			result = new int[1];
			result[0] = obj;
		}
		return result;
	}
}
